<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
          <!-- 姓名 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 工号是系统生成的  禁用这个组件-->
                <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="userInfo.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="!!$route.params.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 会单独封装-->
                <el-cascader
                  size="mini"
                  class="inputW"
                  :options="options"
                  :props="{ label: 'name', value: 'id' }"
                  separator="-"
                  :value="userInfo.departmentId"
                  @change="handleChange"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片  value + @input -->
                <ImageUpload v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { getTreeFromList } from '@/utils'
import { addEmployee, getEmployeeDetail, editEmployee } from '@/api/employee'
import ImageUpload from './components/image-upload.vue'

export default {
  components: { ImageUpload },
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: '' // 员工头像
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
        //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        // 转正时间
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          // 自定义校验  不能早于隔壁那个timeOfEntry
          validator: (rule, value, callback) => {
            if (this.userInfo.timeOfEntry) {
              if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                callback(new Error('转正时间不能小于入职时间'))
                return
              }
            }
            callback()
          }
        }]
      },
      // 级联 => 树形数据 => label value + children
      options: [
        // {
        //   name: '传智教育', // 文字描述
        //   id: '1', // 提交到数据中的值
        //   // 子节点
        //   children: [
        //     {
        //       name: '总裁办',
        //       id: '2'
        //     },
        //     {
        //       name: '人事部',
        //       id: '3',
        //       children: [
        //         { name: 'A部', id: '4' },
        //         { name: 'B部', id: '5' },
        //         { name: 'C部', id: '6' }
        //       ]
        //     }
        //   ]
        // }
      ]

    }
  },
  async created() {
    // 请求部门列表 => 树形
    const res = await getDepartmentList() // this.xx()
    this.options = getTreeFromList(res, 0)

    // 获取员工详情 => 在编辑的前提下
    if (this.$route.params.id) this.userInfo = await getEmployeeDetail(this.$route.params.id)
  },
  methods: {
    handleChange(list) {
      // list => 你沿途选中的部门id组成的数组
      // console.log('111', list[list.length-1])
      this.userInfo.departmentId = list[list.length - 1]
    },
    saveData() {
      this.$refs.userForm.validate(async(isOk) => {
        if (!isOk) return

        // 请求接口(封装函数) => 提示 => 回到列表页
        if (this.$route.params.id) {
          await editEmployee(this.userInfo)
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
        } else {
          await addEmployee(this.userInfo)
          this.$message({
            type: 'success',
            message: '新增成功'
          })
        }

        this.$router.push('/employee')
      })
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 390px
      }
    }

</style>
